package com.mirror.controller;

import com.mirror.entity.oldman.Employees;
import com.mirror.entity.oldman.Result;
import com.mirror.entity.oldman.SchedulingProgram;
import com.mirror.repository.EmployeesRepository;
import com.mirror.repository.SchedulingProgramRepository;
import com.mirror.util.CommonUtil;
import com.mirror.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Api(description = "班组管理")
@RestController
@RequestMapping("employeesGroup")
public class SchedulingProgramController {


    @Autowired
    private SchedulingProgramRepository schedulingProgramRepository;
    @Autowired
    private EmployeesRepository employeesRepository;


    public JSONObject showSP(SchedulingProgram r) {
        JSONObject jo = new JSONObject();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        jo.put("id", r.getId());
        jo.put("createDate", sdf.format(r.getCreateDate()));
        jo.put("name", r.getName());
        jo.put("roomIds", r.getRoomIds());
        jo.put("roomNames", r.getRoomNames());
        String mname = "";
        String ename = "";
        String[] ses = r.getEmployeesids().split(",");
        for (String se : ses) {
            Employees e=employeesRepository.findOne(se);
            if(e!=null){
                if (ename.length() == 0) {
                    ename = e.getName();
                } else {
                    ename = ename + "," + e.getName();
                }
            }

        }
        jo.put("monitorNames", mname);
        jo.put("employeeNames", ename);
        jo.put("employeeIds", r.getEmployeesids());
        return jo;
    }


    @ApiOperation(value = "班组列表", notes = "班组列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"page\": \"页码\",\"size\": \"每页数量\",\"name\": \"姓名搜索\"}", required = true, dataType = "String"),
    })
    @PostMapping("/pageList")
    public Result pageList(@RequestBody String data) {
        JSONObject tempData = new JSONObject(data);
        int page = tempData.getInt("page");
        int size = tempData.getInt("size");

        Sort sort = new Sort(Sort.Direction.DESC, "createDate");
        Pageable pageable = new PageRequest(page, size, sort);
        Page<SchedulingProgram> atPage = schedulingProgramRepository.findAll(pageable);
        JSONArray ja = new JSONArray();
        for (SchedulingProgram e : atPage) {
            ja.put(showSP(e));
        }
        JSONObject json = new JSONObject();
        json.put("memo", ja);
        json.put("totalPages", atPage.getTotalPages());
        json.put("totalElements", atPage.getTotalElements());
        return ResultUtil.success(json.toString());

    }

    @ApiOperation(value = "班组列表无分页", notes = "班组列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{}", required = true, dataType = "String"),
    })
    @GetMapping("/allList")
    public Result allList() {
        List<SchedulingProgram> atPage = schedulingProgramRepository.findAll();
        return ResultUtil.success(atPage);

    }


    @ApiOperation(value = "添加班组", notes = "添加班组")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"name\":\"方案名称\",\"roomIds\":\"房间id\",\"roomNames\":\"房间名\",\"employeesids\":\"护工id逗号拼\"}", required = true, dataType = "String"),
    })
    @PostMapping("/save")
    public Result save(@RequestBody String data) {
        JSONObject tempData = new JSONObject(data);
        String roomIds = tempData.optString("roomIds");
        String name = tempData.optString("name");
        String roomNames = tempData.optString("roomNames");
        String employeeIds = tempData.optString("employeeIds");
        SchedulingProgram sp = new SchedulingProgram();
        sp.setCreateDate(new Date());
        sp.setUpdateDate(new Date());
        sp.setName(name);
        sp.setRoomIds(roomIds);
        sp.setRoomNames(roomNames);
        sp.setEmployeesids(employeeIds);
        schedulingProgramRepository.save(sp);
        if(CommonUtil.isNOT_Null(employeeIds)){
            String[] ids=employeeIds.split(",");
            for (String se : ids) {
                Employees e=employeesRepository.findOne(se);
                if(e!=null){
                   e.setSchProgram(sp);
                    employeesRepository.save(e);
                }
            }
        }
        return ResultUtil.success();
    }

    @ApiOperation(value = "修改班组", notes = "修改班组")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"id\":\"班组id\",\"name\":\"方案名称\",\"roomIds\":\"房间id\",\"roomNames\":\"房间名\",\"employeesids\":\"护工id逗号拼\"}", required = true, dataType = "String"),
    })
    @PostMapping("/edit")
    public Result edit(@RequestBody String data) {
        JSONObject tempData = new JSONObject(data);
        String id = tempData.optString("id");
        String roomIds = tempData.optString("roomIds");
        String name = tempData.optString("name");
        String roomNames = tempData.optString("roomNames");
        String employeeIds = tempData.optString("employeeIds");
        SchedulingProgram sp = schedulingProgramRepository.findOne(id);
        if (sp == null) {
            return ResultUtil.error(-1, "获取班组失败！");
        }
        List<Employees> es=employeesRepository.findAllBySchProgramAndIsdel(sp,false);
        for(Employees e:es){
            e.setSchProgram(null);
            employeesRepository.save(e);
        }
        sp.setUpdateDate(new Date());
        sp.setName(name);
        sp.setRoomIds(roomIds);
        sp.setRoomNames(roomNames);
        sp.setEmployeesids(employeeIds);
        schedulingProgramRepository.save(sp);
        if(CommonUtil.isNOT_Null(employeeIds)){
            String[] ids=employeeIds.split(",");
            for (String se : ids) {
                Employees e=employeesRepository.findOne(se);
                if(e!=null){
                    e.setSchProgram(sp);
                    employeesRepository.save(e);
                }
            }
        }
        return ResultUtil.success();
    }
    @ApiOperation(value = "删除班组", notes = "删除班组")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"id\":\"班组id\"}", required = true, dataType = "String"),
    })
    @PostMapping("/del")
    public Result del(@RequestBody String data) {
        JSONObject tempData = new JSONObject(data);
        String id = tempData.optString("id");
        SchedulingProgram sp = schedulingProgramRepository.findOne(id);
        if (sp == null) {
            return ResultUtil.error(-1, "获取班组失败！");
        }
        List<Employees> es=employeesRepository.findAllBySchProgramAndIsdel(sp,false);
        for(Employees e:es){
            e.setSchProgram(null);
            employeesRepository.save(e);
        }
        schedulingProgramRepository.delete(sp);
        return ResultUtil.success();
    }


}

